<%@ page import="java.sql.*" %>
<%@ page import="dbpool.*" %>
<%--
  Created by IntelliJ IDEA.
  User: maoma
  Date: 2024/4/10
  Time: 9:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>DBCP数据库连接池</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String User = request.getParameter("username");     //得到登录用户名
    String Pwd = request.getParameter("password");       //得到登录密码
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    //String url = "jdbc:mysql://localhost:3306/newsdb?useUnicode=true&characterEncoding=utf-8";
//1加载JDBC驱动      加
    try {
      //  Class.forName("com.mysql.jdbc.Driver");
//2建立与数据库的连接,从数据库连接池取连接    连
        conn = DbcpConnectionPool2.getConnection();

//3创建操作数据库SQL的Statement对象      预
        String sql = "select password from users where username='" + User + "'";
        stmt = conn.createStatement();
//4执行SQL语句，并分析执行结果             执
        rs = stmt.executeQuery(sql);
        if (rs.next())   //判断数据库是否存在此用户
        { //判断用户输入密码是否正确
            if (Pwd.equals(rs.getString(1))) {
                session.setAttribute("username", User);
                //重定向跳转到main.jsp页面
                response.sendRedirect("main.jsp");
            } else {
                out.println("密码错误!!");
            }
        } else {
            out.println("<script LANGUAGE='javascript'>alert('用户名错误!!');history.go(-1);</script>");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
//5关闭连接，释放资源       释
   DbcpConnectionPool2.close(rs,stmt,conn);
   //归还连接
    }
%>
</body>
</html>
